package com.intct.hbase.dml;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-08-06 15:34
 */
public class DeleteCommand {
    public static void main(String[] args) throws IOException {
        // 创建HBase连接
        Connection connection = ConnectionFactory.createConnection();
        
        // TableName对象
        TableName tableName = TableName.valueOf("intct83", "t5");

        // admin对象
        Admin admin = connection.getAdmin();
        Table table = null;

        try {
            // 判断表存在
            if (admin.tableExists(tableName)) {
                // table对象
                table = connection.getTable(tableName);

                // 删除一行记录
                Delete delete = new Delete("1001".getBytes());
//                // *删除指定列限定符(删除最后一个版本)
//                delete.addColumn("f1".getBytes(), "msg".getBytes());
//
//                // *删除指定列限定符(删除所有版本)
//                delete.addColumns("f1".getBytes(), "msg".getBytes());

                // 执行删除数据
                table.delete(delete);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (table != null) {
                table.close();
            }
            if (admin != null) {
                admin.close();
            }
            if (connection != null) {
                connection.close();
            }
        }}
}
